﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portfolio.IViews.Admin;
using Portfolio.BL.Categories;
using Portfolio.BL.Interfaces;
using Portfolio.Entities;
using Portfolio.Presenters.Interfaces;
using Portfolio.BL.Common;

namespace Portfolio.Presenters.Admin
{
    
    public class AdminEditCategoryPresenter : IDefaultPresenter
    {
        
        private IAdminEditCategoryView _view;        
        private CategoriesBL _categoriesBL;
        private IList<ArticleCategoryT> _categoriesList;        
        

        public AdminEditCategoryPresenter(IAdminEditCategoryView view)
        {
            _view = view;            
            _categoriesBL = new CategoriesBL();            
            this.Initialize();
        }

        public void Initialize()
        {
            this._view.LoadCategory += new EventHandler<EventArgs>(_viewLoadCategory);
            this._view.LoadCategories += new EventHandler<EventArgs>(_viewLoadCategories);
            this._view.LoadStatus += new EventHandler<EventArgs>(_viewLoadStatus);            
            this._view.UpdateCategory += new EventHandler<EventArgs>(_viewUpdateCategory);
            this._view.InsertCategory += new EventHandler<EventArgs>(_viewInsertCategory);           
        }

        private void _viewLoadCategory(object sender, EventArgs e)
        {
            _categoriesList = new List<ArticleCategoryT>();

            _categoriesBL.ReturnArticleCategoryById(_view.IdArticleCategory);
            //_categoriesList.Add(_categoriesBL.ReturnCategoryById(_view.IdArticleCategory));            
            _categoriesList.Add(_categoriesBL.ArticleCategory);

            _view.PublishException = _categoriesBL.InformationException;            
            _view.CategoryDataBind = _categoriesList;           
            
        }

        private void _viewLoadCategories(object sender, EventArgs e)
        {            
            _view.CategoriesDataSource = new IViews.Common.DataSourceContainer("Name", "ArticleCategoryID", _categoriesBL.ReturnArticleCategoriesDDL(), _categoriesBL.ArticleCategory.ArticleCategoryIDID);
            _view.PublishException = _categoriesBL.InformationException;
        }

        private void _viewLoadStatus(object sender, EventArgs e)
        {
            _view.StatusDataSource = new IViews.Common.DataSourceContainer("Value", "Key", _categoriesBL.ReturnStatusEnum(), _categoriesBL.ArticleCategory.Status);                
            _view.PublishException = _categoriesBL.InformationException;            
        }        

        private void _viewUpdateCategory(object sender, EventArgs e)
        {           
            //do analizy    
            _categoriesBL.UpdateArticleCategory(_view.CategoriesData);
            _view.PublishInformationSuccess = _categoriesBL.InformationSuccess;
            _view.PublishException = _categoriesBL.InformationException;                       
        }        
        
        private void _viewInsertCategory(object sender, EventArgs e)
        {
            _view.IdArticleCategory = _categoriesBL.InsertArticleCategory(_view.CategoriesData);
            _view.PublishInformationSuccess = _categoriesBL.InformationSuccess;
            _view.PublishException = _categoriesBL.InformationException;         
        }
        
    }
}

